<?php
/*
 * 负责路由
 */
class Route
{
	protected $_controller,$_action,$_param;
	static $_instance;
    /*
     * @return 无
     * @构造函数 
     */
	private function __construct()
	{
		$splite = explode('/',trim( $_SERVER['REQUEST_URI'],'/'));
		$this->_controller = isset($splite[0]) && $splite[0] ? ucfirst(strtolower($splite[0])).'Controller' : 'IndexController';
		$this->_action = isset($splite[1]) && $splite[1] ? strtolower($splite[1]).'Action' : 'indexAction';
		if(isset($splite[2]) && $splite[2])
		{
			$keys = $values = array();
			for($i = 2, $n = count($splite);$i < $n; $i++)
			{
				if($i % 2 == 0)
				{
					//偶数必是键，奇数为值
					$keys[] = $splite[$i];
				}else{
					$values[] = $splite[$i];
				}
			}
			@$this->_param = array_combine($keys, $values);
		}	
	}
    /*
     * @return  object
     * 
     */
	public static function getInstance()
	{
		if(!(self::$_instance instanceof  self))
		{
			self::$_instance = new self;
		}
		return self::$_instance;
	}
	
	/*
	 * 初始化数据
	 */
	public function init()
	{
		if($this->_controller)
		{
			if(class_exists($this->_controller))
			{
				$ctrl = new $this->_controller($this->_param);
				if(method_exists($ctrl, $this->_action))
				{
					session_start();//开启session
					$tmpAction = $this->_action;
					$ctrl->$tmpAction();
				}else {
					die('This Action is not exist ');//跳转错误页
				}
			}else{
				die('This Controller is not exist ');//跳转错误页
			}
		}
	}
}
